from openpyxl import Workbook, load_workbook
from docx import Document
import numpy as np

def xlsx2docx(srcFile, dstFile):
    '''
    param:
        srcFile: 输入待转换的源文件
        dspPath: 输出待转换的目的文件
    '''
    wb = load_workbook(srcFile)
    sheet = wb.active

    print(sheet.dimensions)

    col1 = sheet['A']
    col2 = sheet['B']

    for row in col1:
        print(row.value)

    for row in col2:
        print(row.value)

    #docx部分,doc则需要转换
    # 实例化

    document = Document(dstFile)
    # 读取word中的所有表格
    tables = document.tables
    print(len(tables))

    for i in np.arange(0, 3):
        tables[0].cell(0, i).text = col1[i].value

    for i in np.arange(0, 3):
        tables[0].cell(1, i).text = str(col2[i].value)

    document.save(dstFile)

if __name__ == '__main__':
    #xlsx 读取部分，如果是xls则需要转换
    srcFile = "e://xlsx2docx//test//src.xlsx"
    dstFile = "e://xlsx2docx//test//dst.docx"
    xlsx2docx(srcFile, dstFile)
